package com.broadengate.action;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.broadengate.form.WithdrawForm;
import com.broadengate.model.Atminfo;
import com.broadengate.model.Card;
import com.broadengate.model.Dailyammount;
import com.broadengate.model.Withdraw;
import com.broadengate.service.AdminService;
import com.broadengate.service.AmmountService;
import com.broadengate.service.CardService;
import com.broadengate.service.CurrencyService;
import com.broadengate.service.WithdrawService;

public class WithdrawAction extends DispatchAction{

	private WithdrawService withdrawService;
	private CardService cardService;
	private CurrencyService currencyService;
	private AdminService adminService;
	private AmmountService ammountService;
	
	/******
	 * 实现用户取款
	 * 核心功能由调用withdrawService对象的方法withdraw();cardService对象的方法findCardByCardNO()；currencyService对象的方法findCurrencyByName实现
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward 对象
	 */
	public ActionForward withdraw(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {			
			
		    HttpSession session=request.getSession();
			System.out.println("WithdrawAction 进入");
			System.out.println(cardService.findAtminfo().getBalance());
			WithdrawForm withdrawForm = (WithdrawForm) form;
			float ammout = Float.parseFloat(withdrawForm.getAmmount());
			
			int i=(int)ammout;
			String cardNO=request.getSession().getAttribute("cardNO").toString();				
			Card cardX=cardService.findCardByCardNO(cardNO);
			Atminfo atminfo=cardService.findAtminfo();
			
			if(ammout>2500){
				session.setAttribute("wrong","bigtwo");
				return mapping.findForward("fail");
			}
			if((Math.abs(ammout-i)>=0.00001f )|| (i%100!=0)){
				session.setAttribute("wrong","notzwd");
				return mapping.findForward("fail");
			}
			if(cardService.findAtminfo().getBalance()<ammout){
			     session.setAttribute("wrong","ATMNE");
			     System.out.println(session.getAttribute("wrong").toString());
			     return mapping.findForward("fail");
            }
			if(cardX.getRmbbalance()<ammout){
				 session.setAttribute("wrong","CNE");
			     System.out.println(session.getAttribute("wrong"));
			     return mapping.findForward("fail");
			}		
////////////////////////////*********************/////////////////////////////			
			if(null==ammountService.findDailyammount(cardNO)){
				Dailyammount dailyammount=new Dailyammount();		
				dailyammount.setCardNo(cardNO);
				dailyammount.setTodayAmmount(ammout);
				ammountService.saveAmmount(dailyammount);
			}
			else{
				Dailyammount dailyammount=ammountService.findDailyammount(cardNO);
				float todayAmmount=dailyammount.getTodayAmmount()+ammout;
				if(todayAmmount<=30000){
					dailyammount.setTodayAmmount(todayAmmount);
					ammountService.updateAmmount(dailyammount);
				}
				else {
					session.setAttribute("wrong","beyond");
					return mapping.findForward("fail");
				}
			}
////////////////////////////*********************/////////////////////////////
			
			String currencyName="rmb";	
					
			if(withdrawForm.getCurrency().equals("rmb")){
				currencyName="人民币";
				cardX.setRmbbalance(cardX.getRmbbalance()-ammout);
				
				atminfo.setBalance(cardService.findAtminfo().getBalance()-ammout);
//				if((cardService.findAtminfo().getBalance()>=ammout)&&(cardX.getRmbbalance()>=ammout)){			    
//					cardX.setRmbbalance(cardX.getRmbbalance()-ammout);
//					atminfo.setBalance(cardService.findAtminfo().getBalance()-ammout);				
//				}
//				else if(cardService.findAtminfo().getBalance()<ammout){
//				     session.setAttribute("wrong","ATMNE");
//				     System.out.println(session.getAttribute("wrong").toString());
//				     return mapping.findForward("fail");
//                }
//				else if(cardX.getRmbbalance()<ammout){
//					 session.setAttribute("wrong","CNE");
//				     System.out.println(session.getAttribute("wrong"));
//				     return mapping.findForward("fail");
//				}
			}
			else if(withdrawForm.getCurrency().equals("dollar")){
				currencyName="美元";
				cardX.setDollarBalance(cardX.getDollarBalance()-ammout);
			    atminfo.setBalance(cardService.findAtminfo().getBalance()-ammout);
			
//				if((cardService.findAtminfo().getBalance()>=ammout)&&(cardX.getDollarBalance()>=ammout)){
//				    cardX.setDollarBalance(cardX.getDollarBalance()-ammout);
//				    atminfo.setBalance(cardService.findAtminfo().getBalance()-ammout);
//					
//				}
//				else if(cardService.findAtminfo().getBalance()<ammout){
//				     session.setAttribute("wrong","ATMNE");
//				     System.out.println(session.getAttribute("wrong").toString());
//				     return mapping.findForward("fail");
//               }
//				else if(cardX.getRmbbalance()<ammout){
//					 session.setAttribute("wrong","CNE");
//				     System.out.println(session.getAttribute("wrong"));
//				     return mapping.findForward("fail");
//				}
			}
			else if(withdrawForm.getCurrency().equals("euro")){
				currencyName="欧元";
				cardX.setEuroBalance(cardX.getEuroBalance()-ammout);
				atminfo.setBalance(cardService.findAtminfo().getBalance()-ammout);
				
//				if((cardService.findAtminfo().getBalance()>=ammout)&&(cardX.getEuroBalance()>=ammout)){
//				    cardX.setEuroBalance(cardX.getEuroBalance()-ammout);
//				    atminfo.setBalance(cardService.findAtminfo().getBalance()-ammout);
//					
//				}
//				else if(cardService.findAtminfo().getBalance()<ammout){
//				     session.setAttribute("wrong","ATMNE");
//				     System.out.println(session.getAttribute("wrong").toString());
//				     return mapping.findForward("fail");
//               }
//				else if(cardX.getRmbbalance()<ammout){
//					 session.setAttribute("wrong","CNE");
//				     System.out.println(session.getAttribute("wrong"));
//				     return mapping.findForward("fail");
//				}		
			}
							
			Withdraw withdraw=new Withdraw();
			withdraw.setAmount(ammout);
			withdraw.setCard(cardX);
			withdraw.setCurrency(currencyService.findCurrencyByName(currencyName));
			withdraw.setTime(new Date());
			
			
			try {
				adminService.updateAtminfo(atminfo);
				withdrawService.withdraw(cardX, withdraw);
				return mapping.findForward("success");
			} catch (Exception e) {
				e.printStackTrace();
				return mapping.findForward("fail");
			}
	}

	public CardService getCardService() {
		return cardService;
	}

	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public WithdrawService getWithdrawService() {
		return withdrawService;
	}

	public void setWithdrawService(WithdrawService withdrawService) {
		this.withdrawService = withdrawService;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public AmmountService getAmmountService() {
		return ammountService;
	}

	public void setAmmountService(AmmountService ammountService) {
		this.ammountService = ammountService;
	}


}


